<template>
	<div>
		<input type="text" v-model="title" class="title" placeholder="请输入标题">
		<textarea class="textarea" placeholder="请在此输入内容" v-model="text"></textarea>
		<!-- <image class="timg" src="http://rb1oc9tg0.hn-bkt.clouddn.com/dewu/blog/2022-05-08/20:31:46u51hh9.png" alt="123"></image> -->
		<image class="timg" :src="picture" v-if="picture != null"></image>
		
		<button @click="upload()" style="width: 30vw;font-size: 10px;float: right;">添加图片</button>
		<button @click="send()">发布</button>
	</div>
</template>

<script>
	var picture = "null"

	// console.log("http://" + picture.slice(50,-29))
	export default{
		name:"addBlog",
		data(){
			return{
				picture:picture,
				text:"",
				title:""
			}
		},
		onLoad:function(){
			this.picture = "null"
		},
		methods:{
			upload(){
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: 'http://'+ getApp().globalData.IpAddr + '/upload/blog',
							filePath: tempFilePaths[0],
							success: res => {
								console.log("http://" + JSON.stringify(res).slice(55,-30))
								picture = "http://" + JSON.stringify(res).slice(55,-30)
								this.picture = "http://" + JSON.stringify(res).slice(55,-30)
							},
							fail:res=>{
								console.log(res)
							}
							
						});	
						
						
					}
				});
			},
			send(){
				if(this.text.length == 0 || this.title.length == 0 ){
					wx.showToast({	title: '内容不能为空',	duration: 2000	})
				}else{
					uni.request({
						url:'http://'+ getApp().globalData.IpAddr + '/blog',
						method:"POST",
						header:{
							'authorization' : getApp().globalData.token
						},
						data:{
							'title' : this.title,
							'content' : this.text,
							'images' :this.picture,
							'goodsId' : getApp().globalData.goodsId,
							'isMoney' : 0
						},
						success:res=>{
							// console.log(res)
							if(!res.data.success) wx.showToast({	title: res.data.errorMsg,	duration: 2000	})
							else {
								wx.showToast({	title: '上传成功',	duration: 2000	})
								
							}
						}
					})
				}

			}
		}
	}
</script>

<style>
	.title{
		/* border:1px solid red; */
		margin: 5vw;
		width: 90vw;
		border-radius: 10px;
	}
	.textarea{
		/* border:1px solid red; */
		margin: 5vw;
		width: 90vw;
		border-radius: 10px;
	}
	.avc{
		/* background-color: red; */
		width: 10vw;
		height: 10vw;
	}
	.timg{
		height: 30vw;
		width: 30vw;
	}
</style>